//
//  HLMessageMaker.m
//  YueYue
//
//  Created by hulu001 on 2023/6/12.
//

#import "HLMessageMaker.h"

@implementation HLMessageMaker

+ (NIMMessage*)msgWithText:(NSString*)text
{
    NIMMessage *textMessage = [[NIMMessage alloc] init];
    textMessage.text        = text;
    textMessage.remoteExt = @{
        @"isRechargeable":@(1),
        @"type":@(21),
        @"discount":@"0"
    };
    [self setupMessage:textMessage];
    return textMessage;
}

+ (NIMMessage *)msgWithVideo:(NSString *)filePath discount:(nonnull NSString *)discount
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    NIMVideoObject *videoObject = [[NIMVideoObject alloc] initWithSourcePath:filePath];
    videoObject.displayName = [NSString stringWithFormat:@"%@%@",@"视频发送于",dateString];
    NIMMessage *message = [[NIMMessage alloc] init];
    message.messageObject = videoObject;
    message.apnsContent = @"发来了一段视频";
    NIMMessageSetting *setting = [[NIMMessageSetting alloc] init];
    setting.scene = NIMNOSSceneTypeMessage;
    message.setting = setting;
    message.env = @"test";
    NSMutableDictionary *remoteExt = [NSMutableDictionary dictionary];
    remoteExt[@"isRechargeable"] = @(1);
    remoteExt[@"type"] = @(21);
    if (discount) {
        if (![discount isEqualToString:@"100"]) {
            remoteExt[@"discount"] = discount;
        }
    }
    message.remoteExt = remoteExt;
    return message;
}

+ (NIMMessage*)msgWithImage:(UIImage*)image discount:(nonnull NSString *)discount
{
    NIMImageObject *imageObject = [[NIMImageObject alloc] initWithImage:image scene:NIMNOSSceneTypeMessage];
    NIMImageOption *option  = [[NIMImageOption alloc] init];
    option.compressQuality  = 0.7;
    imageObject.option      = option;
    return [self generateImageMessage:imageObject discount:discount];
}

+ (NIMMessage *)generateImageMessage:(NIMImageObject *)imageObject discount:(nonnull NSString *)discount
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    imageObject.displayName = [NSString stringWithFormat:@"图片发送于%@",dateString];
    NIMMessage *message     = [[NIMMessage alloc] init];
    message.messageObject   = imageObject;
    message.apnsContent = @"发来了一张图片";
    [self setupMessage:message];
    NSMutableDictionary *remoteExt = [NSMutableDictionary dictionary];
    remoteExt[@"isRechargeable"] = @(1);
    remoteExt[@"type"] = @(21);
    if (discount) {
        if (![discount isEqualToString:@"100"]) {
            remoteExt[@"discount"] = discount;
        }
    }
    message.remoteExt = remoteExt;
    return message;
}

/// 发送文本消息
/// - Parameters:
///   - text: 云信text
///   - discount: 聊天扣费类型 0:付费聊天 1:聊天卡 100:免费
+ (NIMMessage *)msgWithText:(NSString *)text discount:(NSString *)discount
{
    NIMMessage *textMessage = [[NIMMessage alloc] init];
    textMessage.text        = text;
    NSMutableDictionary *remoteExt = [NSMutableDictionary dictionary];
    remoteExt[@"isRechargeable"] = @(1);
    remoteExt[@"type"] = @(21);
    if (discount) {
        if (![discount isEqualToString:@"100"]) {
            remoteExt[@"discount"] = discount;
        }
    }
    textMessage.remoteExt = remoteExt;
    [self setupMessage:textMessage];
    return textMessage;
}

+ (NIMMessage*)msgWithAudio:(NSString*)filePath
{
    NIMAudioObject *audioObject = [[NIMAudioObject alloc] initWithSourcePath:filePath scene:NIMNOSSceneTypeMessage];
    NIMMessage *message = [[NIMMessage alloc] init];
    message.messageObject = audioObject;
    message.text = @"发来了一段语音";
    message.remoteExt = @{
        @"isRechargeable":@(1),
        @"type":@(21),
        @"discount":@"0"
    };
    [self setupMessage:message];
    return message;
}

+ (NIMMessage *)msgWithVideo:(NSString *)filePath
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    NIMVideoObject *videoObject = [[NIMVideoObject alloc] initWithSourcePath:filePath];
    videoObject.displayName = [NSString stringWithFormat:@"%@%@",@"视频发送于",dateString];
    NIMMessage *message = [[NIMMessage alloc] init];
    message.messageObject = videoObject;
    message.apnsContent = @"发来了一段视频";
    NIMMessageSetting *setting = [[NIMMessageSetting alloc] init];
    setting.scene = NIMNOSSceneTypeMessage;
    message.setting = setting;
    message.env = @"test";
    message.remoteExt = @{
        @"isRechargeable":@(1),
        @"type":@(21),
        @"discount":@"0"
    };
    return message;
}

+ (NIMMessage *)msgWithVideoData:(NSData *)data
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    NIMVideoObject *videoObject = [[NIMVideoObject alloc] initWithData:data extension:@""];
    videoObject.displayName = [NSString stringWithFormat:@"%@%@",@"视频发送于",dateString];
    NIMMessage *message = [[NIMMessage alloc] init];
    message.messageObject = videoObject;
    message.apnsContent = @"发来了一段视频";
    NIMMessageSetting *setting = [[NIMMessageSetting alloc] init];
    setting.scene = NIMNOSSceneTypeMessage;
    message.setting = setting;
    message.env = @"test";
    message.remoteExt = @{
        @"isRechargeable":@(1),
        @"type":@(21),
        @"discount":@"0"
    };
    return message;
}

+ (NIMMessage *)msgWithTip:(NSString *)tip
{
    NIMMessage *message        = [[NIMMessage alloc] init];
    NIMTipObject *tipObject    = [[NIMTipObject alloc] init];
    message.messageObject      = tipObject;
    message.text               = tip;
    NIMMessageSetting *setting = [[NIMMessageSetting alloc] init];
    setting.apnsEnabled        = NO;
    setting.shouldBeCounted    = NO;
    message.env                = @"test";
    message.setting            = setting;
//    message.remoteExt = @{
//        @"isRechargeable":@(1),
//        @"type":@(21),
//        @"discount":@"0"
//    };
    return message;
}

+ (NIMMessage *)msgWithCustomObject:(id<NIMCustomAttachment>)attachment
{
    NIMCustomObject *object = [NIMCustomObject.alloc init];
    object.attachment = attachment;
    
    NIMMessageSetting *setting = [[NIMMessageSetting alloc] init];
    setting.shouldBeCounted = NO;
    setting.historyEnabled = YES;
    
    NIMMessage *tipMessage = [NIMMessage.alloc init];
    tipMessage.env = @"test";
    tipMessage.messageObject = object;
    tipMessage.setting = setting;
    tipMessage.timestamp = [[NSDate date] timeIntervalSince1970];
    
    return tipMessage;
}

+ (NIMMessage*)msgWithImage:(UIImage*)image
{
    NIMImageObject *imageObject = [[NIMImageObject alloc] initWithImage:image scene:NIMNOSSceneTypeMessage];
    NIMImageOption *option  = [[NIMImageOption alloc] init];
    option.compressQuality  = 0.7;
    imageObject.option      = option;
    return [self generateImageMessage:imageObject];
}

+ (NIMMessage*)msgWithImage:(UIImage *)image url:(NSString *)url
{
    NIMImageObject *imageObject = [[NIMImageObject alloc] initWithImage:image scene:NIMNOSSceneTypeMessage];
    [imageObject setUploadURL:url];
    NIMImageOption *option  = [[NIMImageOption alloc] init];
    option.compressQuality  = 0.7;
    option.format = NIMImageFormatPNG;
    imageObject.option      = option;
    return [self generateImageMessage:imageObject];
}

+ (NIMMessage *)msgWithAttachment:(id<NIMCustomAttachment>)attchment
{
    NIMCustomObject *messageObject = [NIMCustomObject.alloc init];
    messageObject.attachment = attchment;
    
    NIMMessage *message = [[NIMMessage alloc] init];
    message.messageObject = messageObject;
    message.text = @"[自定义]";
//    message.remoteExt = @{
//        @"isRechargeable":@(1),
//        @"type":@(21),
//        @"discount":@"0"
//    };
    [self setupMessage:message];
    return message;
}

+ (NIMMessage *)generateImageMessage:(NIMImageObject *)imageObject
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    imageObject.displayName = [NSString stringWithFormat:@"图片发送于%@",dateString];
    NIMMessage *message     = [[NIMMessage alloc] init];
    message.messageObject   = imageObject;
    message.apnsContent = @"发来了一张图片";
    [self setupMessage:message];
    return message;
}

+ (void)setupMessage:(NIMMessage *)message
{
    message.apnsPayload = @{
        @"apns-collapse-id": message.messageId,
    };
    message.env = @"test";
    NIMMessageSetting *setting = [[NIMMessageSetting alloc] init];
    setting.scene = NIMNOSSceneTypeMessage;
    message.setting = setting;
}
@end
